package com.xishu.util;


import com.xishu.config.Config;
import com.xishu.exception.ResponseStatusException;
import com.xishu.response.VerifyUtil;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class MongoId implements Tools {
    private static Logger logger = LoggerFactory.getLogger(MongoId.class);

    private static MongoId instance = new MongoId();

    private MongoId() {
        this.instance = instance;
    }

    public static MongoId getInstance() {
        return instance;
    }

    public static String generateId() {
        return new ObjectId().toString();
    }

    /**
     * 生成临时文件
     *
     * @return
     */
    public File generateTmpFile(String suffix) throws ResponseStatusException {
        VerifyUtil.verify(() -> isNotEmpty(suffix));
        Config config = Config.getInstance();
        File file = new File(config.getUploadTmpPath() + "/" + generateId() + "." + suffix);
        logger.info("tmp file path is {}", file.getAbsolutePath());
        return file;
    }

    /**
     * 生成临时文件
     *
     * @return
     */
    public File generateTmpFileWithName(String suffix, String fileName) throws ResponseStatusException {
        VerifyUtil.verify(() -> isNotEmpty(suffix));
        Config config = Config.getInstance();
        String fileSuffix = DateUtil.format(System.currentTimeMillis(), "yyyy-MM-dd-HH-mm-ss");
        File file = new File(config.getUploadTmpPath() + "/" + fileName + "-" + fileSuffix + "." + suffix);
        logger.info("tmp file path is {}", file.getAbsolutePath());
        return file;
    }

    /**
     * 生成一个固定名称的文件
     *
     * @param fileName
     * @return
     */
    public File generateTmpFileWithName(String fileName) {
        Config config = Config.getInstance();
        File file = new File(config.getUploadTmpPath() + "/" + fileName);
        return file;
    }


}
